package com.mzj.saas.mservice.property.repository;

import com.mzj.saas.mservice.property.entity.EstateRentBill;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import javax.persistence.OrderBy;
import java.util.List;

public interface EstateRentBillRepository extends PagingAndSortingRepository<EstateRentBill, Long>, JpaSpecificationExecutor<EstateRentBill>, JpaRepository<EstateRentBill, Long> {
	List<EstateRentBill> findByIdIn(List<Long> ids);

	List<EstateRentBill> findByHouseIdAndPaymentStatusAndAuditStatus(String houseId,Integer paymentStatus,Integer auditStatus);


	List<EstateRentBill> findByHouseId(String houseId);

	@OrderBy("create_time desc")
	List<EstateRentBill> findByRuleId(String ruleId);

	@Query(value = "SELECT * FROM saas_estate_rent_bill WHERE house_id = ?1 and  charge_type = ?2 and org_id = ?3 and audit_status = 1 and  payment_status != 1 ORDER BY create_time " ,nativeQuery = true)
	List<EstateRentBill> findByHouseIdAndChargeTypeAndOrgId(String houseId, Integer chargeType, Long orgId);

	@Query(value = "SELECT * FROM saas_estate_rent_bill WHERE house_id = ?1 and org_id = ?2 and audit_status = 1 and  payment_status != 1 ORDER BY create_time " ,nativeQuery = true)
	List<EstateRentBill> findByHouseIdAndOrgId(String houseId,  Long orgId);

	@Query("select ee from EstateRentBill ee where ee.auditStatus = ?1 and  ee.paymentStatus in (?2,2)  and  ee.houseId = ?3 and ee.orgId = ?4 order by ee.createTime asc")
	List<EstateRentBill> findByAuditStatusAndPaymentStatusAndHouseIdAndOrgId(Integer auditStatus, Integer paymentStatus, String houseId,Long orgId);

	List<EstateRentBill> findByPaymentStatusAndHouseIdAndOrgId(Integer paymentStatus, String houseId, Long orgId);

	EstateRentBill findByIdAndPaymentStatus(Long billId, int paymentStatus);
}
